const db = require('../config/db')


//登录
module.exports.login = function(data){
    let sql = "select * from userinfo where username = ? and password = ?"
    return db.base(sql,data)
}

//查询所有冉冉商城商品
module.exports.selectrrgoods = function(){
    let sql = 'select * from ranrangoods'
    return db.base(sql,null)
}

//根据页码查询
module.exports.srrgoodsforpage = function(data){
    let sql = "select * from ranrangoods limit ?,?"
    return db.base(sql,data)
}

//新增商品
module.exports.addrrgoods = function(data){
    let sql = "insert into ranrangoods (goodsname,goodsimg,price,tag) values(?,?,?,?)"
    return db.base(sql,data)
}

//编辑商品
module.exports.updategoods = function(data){
    let sql = "update ranrangoods set goodsname = ?, goodsimg = ?, price = ?, tag = ? where id = ?"
    return db.base(sql,data)
}

//删除商品
module.exports.deleterrgoods = function(data){
    let sql = "delete from ranrangoods where id = ?"
    return db.base(sql,data)
}

//删除多件商品
module.exports.deleteMorerrgoods = function(data){
    let ids = data.join(",")
    let sql = "delete from ranrangoods where id in ("+ ids +")"
    return db.base(sql,data)
}

//查询累计销量
module.exports.searchsalescount = function(){
    let sql = 'select sum(sales) as salescount from ranrangoods'
    return db.base(sql)
}

//查询各商品的销量
module.exports.searchgoodssales = function(){
    let sql = 'select goodsname,sales from ranrangoods'
    return db.base(sql)
}

//购买商品
module.exports.buygoods = function(data){
    let sql = 'update ranrangoods set sales = sales + 1 where id = ?'
    return db.base(sql,data)
}